<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <h1>父组件</h1>
        <h1>{{rname}}</h1>
        <ul>
            <li v-for="(item,index) in rlist" :key="index">{{item}}</li>
        </ul>
        <zi-com @senddata="getdata"></zi-com>
    </div>
    <!-- 子组件的模板 -->
    <template id="zi">
      <div>
          <h1>我是子组件</h1>
          <button @click="chuan()">传参</button>
      </div>
    </template>
    <script src="./vue.js"></script>
    <script>
        // vue的子组件像父组件传参  
        // 1父组件自定义事件 
        // 2子组件用$emit()触发父组件的自定义事件并传参
        // 3 父组件的事件函数中的形参就是传来的数据
        new Vue({
            el: "#app",
            data() {
                return {
                    rlist: "", //准备 接受子组件的数据
                    rname: ""
                }
            },
            methods: {
                getdata(a, b) {
                    // a 传来的list 
                    // b 传来的myname
                    this.rlist = a;
                    this.rname = b;
                }
            },
            components: {
                "ziCom": {
                    template: "#zi",
                    data() {
                        return {
                            list: [1, 2, 3],
                            myname: "王德发"
                        }
                    },
                    methods: {
                        chuan() { //点击的时候传参
                            // 2 子组件用$emit() 触发父组件的自定义事件并传参
                            // this.$emit("自定义事件名",参数1，参数2，参数3)
                            // this.$emit("senddata", this.list, this.myname)
                        }
                    },
                    mounted() {
                        this.$emit("senddata", this.list, this.myname)
                    },
                }
            }
        })
    </script>
</body>

</html>